package org.thinkinbunny.financial.activity;

import org.json.JSONArray;
import org.json.JSONObject;
import org.thinkinbunny.financial.R;
import org.thinkinbunny.financial.http.HttpTools;
import org.thinkinbunny.financial.util.Constant;
import org.thinkinbunny.financial.util.Log;
import org.thinkinbunny.financial.util.ShowTools;
import org.thinkinbunny.financial.view.ShowDialog;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class AboutActivity extends MyActivity {

	Button btnUpdate, btnCancel;
	TextView id_companyTitle,id_aboutTitle;
	private ProgressDialog mpDialog;
	public Handler mHandler = new Handler() {
		@Override
		public void handleMessage(android.os.Message msg) {

			switch (msg.what) {
			case 0:
				mpDialog.show();
				break;
			case 1:
				mpDialog.dismiss();
				break;

			}
		};
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub

		setContentView(R.layout.about);
		init_UI();
		super.onCreate(savedInstanceState);
	}

	void init_UI() {

		mpDialog = new ProgressDialog(AboutActivity.this);
		mpDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);// 设置风格为圆形进度条
		mpDialog.setTitle(getString(R.string.checkupdate));// 设置标题
		// mpDialog.setIcon(R.drawable.ic_launcher);// 设置图标
		mpDialog.setMessage(getString(R.string.checkupdate_msg));
		mpDialog.setIndeterminate(false);// 设置进度条是否为不明确

		mpDialog.setCancelable(false);// 设置进度条是否可以按退回键取消
		btnUpdate = (Button) findViewById(R.id.btnUpdate);
		id_companyTitle = (TextView) findViewById(R.id.id_companyTitle);
		id_aboutTitle=(TextView) findViewById(R.id.id_aboutTitle);
		btnUpdate.setOnClickListener(this);
		btnCancel = (Button) findViewById(R.id.btnCancel);
		btnCancel.setOnClickListener(this);
		id_companyTitle.setText(getVersionName());
		id_aboutTitle.setText(getSoftwareName());
	}

	boolean checkUpdate() {

		Message msgMessage = mHandler.obtainMessage(0);
		mHandler.sendMessage(msgMessage);
		// Sync2Server();
		// SyncFromServer();
		try {
			SharedPreferences sharedPreferences = getSharedPreferences(
					Constant.DeviceInfo, Context.MODE_PRIVATE);
			JSONArray updateArray = HttpTools
					.HttpGetDataCheckVer(sharedPreferences.getString(
							"deviceInfo", ""));
			String version=sharedPreferences.getString(
					"version", "");
			JSONObject updateArr = updateArray.getJSONObject(0);
			// .replace("\\/", "/"));
			Log.i("AboutActivity", updateArray.toString());
			Log.i("AboutActivity",
					updateArr.getString("updateUrl").replace("\\", ""));
			String updatecontent = updateArr.getString("updatecontent");
			if (!version.equals(updateArr.getString("version"))) {
				ShowTools.toastInThread("发现新版本" + updatecontent);
				// ShowTools.toast("发现新版本" + updatecontent);
				msgMessage = mHandler.obtainMessage(1);
				mHandler.sendMessage(msgMessage);
				ShowDialog.buildUpdateDialog(AboutActivity.this,
						updateArr.getString("version"), updatecontent,
						updateArr.getString("updateUrl").replace("\\", ""))
						.show();
				return true;
			} else if (version.equals(updateArr.getString("version"))) {
				ShowTools.toastInThread(getString(R.string.version_last)
						+ updateArr.getString("version"));
				// ShowTools.toast("发现新版本" + updatecontent);
				msgMessage = mHandler.obtainMessage(1);
				mHandler.sendMessage(msgMessage);

			} else {
				ShowTools.toastInThread(getString(R.string.update_exception));
				// ShowTools.toast("发现新版本" + updatecontent);
				msgMessage = mHandler.obtainMessage(1);
				mHandler.sendMessage(msgMessage);
			}
		} catch (Exception e) {
			e.printStackTrace();
			msgMessage = mHandler.obtainMessage(1);
			mHandler.sendMessage(msgMessage);
			ShowTools.toastInThread(getString(R.string.update_error));
			// TODO: handle exception
			return false;
		}
		return true;
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		Message msgMessage;
		switch (v.getId()) {
		case R.id.btnUpdate:
			new Thread() {
				@Override
				public void run() {
					// TODO Auto-generated method stub
					Looper.prepare();
					// 此处的Looper必须添加
					checkUpdate();

					Looper.loop();

					super.run();
				}
			}.start();

			// msgMessage = mHandler.obtainMessage(1);
			// mHandler.sendMessage(msgMessage);
			break;
		// case R.id.Recovery_BTN:
		// msgMessage = mHandler.obtainMessage(0);
		// mHandler.sendMessage(msgMessage);
		// // Sync2Server();
		// SyncFromServer();
		// break;

		case R.id.btnCancel:
			finish();
			break;

		default:
			break;
		}
		super.onClick(v);
	}

	private String getVersionName() {
		String version = getString(R.string.softversion);
		// 获取packagemanager的实例
		try {
			PackageManager packageManager = getPackageManager();
			// getPackageName()是你当前类的包名，0代表是获取版本信息
			PackageInfo packInfo = packageManager.getPackageInfo(
					getPackageName(), 0);
			version += packInfo.versionName;

		} catch (Exception e) {
			// TODO: handle exception
			version+=getString(R.string.softversion_error);
			e.printStackTrace();
		}
		return version;
	}

	private String getSoftwareName() {
		String version = "";
		// 获取packagemanager的实例
		try {
			PackageManager packageManager = getPackageManager();
			// getPackageName()是你当前类的包名，0代表是获取版本信息
			PackageInfo packInfo = packageManager.getPackageInfo(
					getPackageName(), 0);
			version = packInfo.applicationInfo.loadLabel(getPackageManager()).toString();

		} catch (Exception e) {
			// TODO: handle exception
			version+=getString(R.string.softversion_error);
			e.printStackTrace();
		}
		return version;
	}
}
